package com.lmy.product.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmy.common.utils.PageUtils;
import com.lmy.common.utils.Query;
import com.lmy.product.entity.BrandEntity;
import com.lmy.product.mapper.BrandDao;
import com.lmy.product.service.BrandService;
import com.lmy.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        if (!StringUtils.isEmpty(key)) {
            // 模糊查询 name 和 descript
            wrapper.eq("brand_id", key)
                    .or()
                    .like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            categoryBrandRelationService.udpateBrand(brand);
            //TODO 更新其他关联
        }
    }

    @Override
    public void test(Map<String,Object> params) {
/*        List<BrandEntity> list = lambdaQuery().eq(BrandEntity::getBrandId, params.get("id"))
                .select(BrandEntity::getName).list();*/
      //  List<BrandEntity> list = lambdaQuery().like(BrandEntity::getName, params.get("name")).list();

       IPage<BrandEntity> iPage =  brandDao.findall(new Page<>(1, 20));
       iPage.setPages(Long.parseLong(params.get("page").toString()));
        System.out.println("添加数据为：。。。"+Long.parseLong(params.get("page").toString()));
        System.out.println("实际数据为。。。。"+iPage.getPages());
        for (long i = 1; i <= iPage.getPages(); i++) {
            IPage<BrandEntity> page =  brandDao.findall(new Page<>(i, 20));

        }
       // System.out.println(list);
    }

}